<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<html>
<head>
    <title>登录</title>
    <script type="text/javascript" src="${ctx}/static/jquery-1.9.1.min.js"></script>
    <script type="text/javascript">
        var basePath = '${ctx}';
        var $userName,$password;
        $(function () {
            $userName = $("#userName");
            $password = $("#password");
            console.log(getCookie('user'));
            //页面初始化时，如果帐号密码cookie存在则填充
            if(getCookie('user') && getCookie('pswd')){
                $userName.val(getCookie('user'));
                $password.val(getCookie('pswd'));
            }else {
                $userName.focus();
            }

            $("#loginBtn").on("click",function () {
                var username = $userName.val();
                var password = $password.val();
                //保存帐号到cookie，有效期7天
                setCookie('user',username,7);
                //保存密码到cookie，有效期7天
                setCookie('pswd',password,7);
            })
        });

        //设置cookie
        function setCookie(name,value,day){
            var date = new Date();
            date.setDate(date.getDate() + day);
            document.cookie = name + '=' + value + ';expires='+ date;
        }
        //获取cookie
        function getCookie(name){
            var reg = RegExp(name+'=([^;]+)');
            var arr = document.cookie.match(reg);
            if(arr){
                return arr[1];
            }else{
                return '';
            }
        }
        //删除cookie
        function delCookie(name){
            setCookie(name,null,-1);
        }
    </script>
</head>
<body>
<form action="login" method="post">
    <input type="text" id="userName" name="userName" placeholder="用户名" value="${userName}"/>
    <input type="password" id="password" name="password" placeholder="密码"/>
    <input type="submit" id="loginBtn" value="登录" />
    <br>
    <span style="color: red;">${error}</span>
</form>
</body>
</html>
